package ee.helmesWorkTest.frontend.controller.menucontrollers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ee.helmesWorkTest.frontend.controller.command.Command;
import ee.helmesWorkTest.frontend.controller.command.PersonSectorInvolvementCommandFactory;
import ee.helmesWorkTest.frontend.controller.eventfinder.EventFinder;
import ee.helmesWorkTest.frontend.controller.eventfinder.PersonSectorEventFinder;

public class PersonSectorMenuController implements MenuController
{
	private final int SUCCESSFUL_OPERATION_RESULT_INDICATOR = 1;
	private final int UNSUCCESSFUL_OPERATION_RESULT_INDICATOR = 0;
	
	@Override
	public String service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		String userView = "insertPersonSectorInvolvementPage";
		EventFinder eventFinder = new PersonSectorEventFinder();
		PersonSectorInvolvementCommandFactory personSectorInvolvementCommandFactory = new PersonSectorInvolvementCommandFactory();
		Command command = null ;
		String event = null;
		int operationResult = 1;
		
		try 
		{
			event = eventFinder.findEvent(request, response);
			command = personSectorInvolvementCommandFactory.getCommand(event);
			
			if (command != null)
			{
				operationResult = command.executeCommand(request, response);
			}
			
			
			if (operationResult == SUCCESSFUL_OPERATION_RESULT_INDICATOR
					&& event.equals("insertPersonSectorInvolvementPage"))
			{
				userView = "insertPersonSectorInvolvementPage";
			}
			
			if (operationResult == UNSUCCESSFUL_OPERATION_RESULT_INDICATOR
					&& event.equals("insertPersonSectorInvolvementPage"))
			{
				userView = "insertPersonSectorInvolvementPage";
			}
			
			
			if (operationResult == SUCCESSFUL_OPERATION_RESULT_INDICATOR
					&& event.equals("insertPersonSectorInvolvementForm"))
			{
				userView = "insertPersonSectorInvolvementPage";
			}
			
			if (operationResult == UNSUCCESSFUL_OPERATION_RESULT_INDICATOR
					&& event.equals("insertPersonSectorInvolvementForm"))
			{
				userView = "insertPersonSectorInvolvementPage";
			}
		}
		catch (Exception ex) 
		{
			ex.printStackTrace();
		}
		
		return userView;
	}
}
